package com.m4f.city.utils.tag.datatable;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class DataTableDataColumnTag extends TagSupport implements IColumnTag {
	private String i18nName;
	private String name;
	private String dataProp;
	
	//
	private String renderCallback;
	
	//
	private boolean bSortable;
	private boolean bSearchable;
	
	
	
	public DataTableDataColumnTag() {
		this.i18nName = "";
		this.name = "";
		this.dataProp = "";
		
		this.renderCallback = "";
		
		this.bSortable = false;
		this.bSearchable = false;
	}
	
	public DataTableDataColumnTag(DataTableDataColumnTag col) {
		this();
		
		this.i18nName = col.getI18nName();
		this.name = col.getName();
		this.dataProp = col.getDataProp();
		
		this.renderCallback = col.getRenderCallback();
		
		this.bSortable = col.isbSortable();
		this.bSearchable = col.isbSearchable();
	}

	@Override
	public int doStartTag() throws JspException {
		this.validateTag();
		
		DataTableTag tag = (DataTableTag) getParent();
		tag.addCol(new DataTableDataColumnTag(this));
		
		return super.doStartTag();
	}
	
	@Override
	public int doEndTag() throws JspException {
		// TODO Auto-generated method stub
		return super.doEndTag();
	}
	
	// Interface methods
	@Override
	public boolean isActionsColumn() {
		return false;
	}
	
	@Override
	public String getColDefinition() {
		//("{ \"sName\": \"actions\", \"fnRender\" : make_crud_actions, \"bSortable\": false, \"bSearchable\": false },");
		StringBuffer sb = new StringBuffer();
		sb.append("{");
		// DataProp
		sb.append("\"mDataProp\": ").append("\"").append(this.getDataProp()).append("\",");
		// sName
		sb.append("\"sName\": \"");
		if(this.getName() != null && !("").equals(this.getName())) sb.append(this.getName());
		else sb.append(this.getDataProp());
		sb.append("\",");
		// fnRender
		if(this.getRenderCallback() != null && !("").equals(this.getRenderCallback())) sb.append("\"fnRender\": ").append(this.getRenderCallback()).append(",");
		
		sb.append("},");
		
		return sb.toString();
	}
	
	public String getJsFunctionDefinition() {
		return "";
	}
	
	private void validateTag() throws JspException {
		if(isBlank(dataProp) && isBlank(renderCallback)) throw new JspException("Can not be unset dataProp and renderCallback properties. Set at least one of them.");
		
		if(isBlank(name) && isBlank(dataProp)) throw new JspException("Can not be unset name if also dataProp is unset.Set dataProp");
		else name = dataProp;
		
	}

	private boolean isBlank(String property) {
		return property == null || ("").equals(property) ? true : false;
	}
	
	// GETTERS AND SETTERS
	public String getI18nName() {
		return i18nName;
	}

	public void setI18nName(String i18nName) {
		this.i18nName = i18nName;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDataProp() {
		return dataProp;
	}

	public void setDataProp(String dataProp) {
		this.dataProp = dataProp;
	}

	public String getRenderCallback() {
		return renderCallback;
	}

	public void setRenderCallback(String renderCallback) {
		this.renderCallback = renderCallback;
	}

	public boolean isbSortable() {
		return bSortable;
	}

	public void setbSortable(boolean bSortable) {
		this.bSortable = bSortable;
	}

	public boolean isbSearchable() {
		return bSearchable;
	}

	public void setbSearchable(boolean bSearchable) {
		this.bSearchable = bSearchable;
	}
}
